home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 26 / AMIGAplus Sonderheft 26 (2000)(Falke)(DE)(Track 1 of 2)[!].iso / PublicDomain / Anwendungen / Amster / Source / url.c < prev   
C/C++ Source or Header  |  1999-03-29  |  4KB  |  150 lines

  1. /*
  2. ** Clickable URL Class
  3. */
  4.  
  5. #include "include/config.h"
  6.  
  7. #include <string.h>
  8.  
  9. #include "include/mui.h"
  10. #include <proto/graphics.h>
  11. #include <proto/openurl.h>
  12.  
  13. #include "include/url.h"
  14.  
  15. struct Library *OpenURLBase;
  16.  
  17. ULONG url_new(struct IClass *cl, Object *obj, struct opSet *msg);
  18. ULONG url_setup(struct IClass *cl, Object *obj, Msg msg);
  19. ULONG url_minmax(struct IClass *cl, Object *obj, struct MUIP_AskMinMax *msg);
  20. ULONG url_draw(struct IClass *cl, Object *obj, struct MUIP_Draw *msg);
  21. ULONG url_cleanup(struct IClass *cl, Object *obj, Msg msg);
  22. ULONG url_clicked(struct IClass *cl, Object *obj, Msg msg);
  23.  
  24.  
  25. MUIF url_dispatch(REG(a0) struct IClass *cl,REG(a2) Object *obj,REG(a1) Msg msg)
  26. {
  27.     switch(msg->MethodID) {
  28.         case OM_NEW:         return(url_new(cl,obj,(APTR)msg));
  29.         case MUIM_Setup:     return(url_setup(cl,obj,(APTR)msg));
  30.         case MUIM_AskMinMax: return(url_minmax(cl,obj,(APTR)msg));
  31.         case MUIM_Draw:      return(url_draw(cl,obj,(APTR)msg));
  32.         case MUIM_Cleanup:   return(url_cleanup(cl,obj,(APTR)msg));
  33.         case URL_CLICKED:    return(url_clicked(cl,obj,(APTR)msg));
  34.     }
  35.     return(DoSuperMethodA(cl,obj,msg));
  36. }
  37.  
  38.  
  39. ULONG url_new(struct IClass *cl, Object *obj, struct opSet *msg)
  40. {
  41.     struct urldata *data;
  42.     char *name, *href;
  43. /*    char *help;*/
  44.  
  45.     name = (char *)GetTagData(URL_NAME,0,msg->ops_AttrList);
  46.     href = (char *)GetTagData(URL_HREF,0,msg->ops_AttrList);
  47. /*    help = (char *)GetTagData(URL_HELP,0,msg->ops_AttrList);*/
  48.  
  49.     obj = (Object *)DoSuperNew(cl,obj,
  50.         MUIA_Font, MUIV_Font_Button,
  51.         MUIA_ShowSelState, FALSE,
  52.         MUIA_InputMode, MUIV_InputMode_RelVerify,
  53.         /* MUIA_ShortHelp, help, */
  54.         TAG_MORE, msg->ops_AttrList);
  55.  
  56.     if(!obj) return(NULL);
  57.  
  58.     data = INST_DATA(cl,obj);
  59.     data->name = name;
  60.     data->href = href;
  61.     data->len = strlen(name);
  62.     data->color = -1;
  63.  
  64.     DoMethod(obj,MUIM_Notify,MUIA_Pressed,FALSE,obj,1,URL_CLICKED);
  65.     return((ULONG)obj);
  66. }
  67.  
  68.  
  69. ULONG url_setup(struct IClass *cl, Object *obj, Msg msg)
  70. {
  71.     struct urldata *data = INST_DATA(cl,obj);
  72.  
  73.     if (!DoSuperMethodA(cl,obj,msg))
  74.         return(FALSE);
  75.  
  76.     data->color = ObtainBestPen(_screen(obj)->ViewPort.ColorMap,0,0,0,OBP_Precision,PRECISION_GUI,TAG_DONE);
  77.  
  78.     return(TRUE);
  79. }
  80.  
  81.  
  82. ULONG url_minmax(struct IClass *cl, Object *obj, struct MUIP_AskMinMax *msg)
  83. {
  84.     struct urldata *data = INST_DATA(cl,obj);
  85.     int x,y;
  86.  
  87.     DoSuperMethodA(cl,obj,(APTR)msg);
  88.  
  89.     x = _font(obj)->tf_XSize * data->len;
  90.     y = _font(obj)->tf_YSize;
  91.  
  92.     msg->MinMaxInfo->MinWidth  += x;
  93.     msg->MinMaxInfo->DefWidth  += x + (x/10);
  94.     msg->MinMaxInfo->MaxWidth  += MUI_MAXMAX;
  95.     msg->MinMaxInfo->MinHeight += y;
  96.     msg->MinMaxInfo->DefHeight += y;
  97.     msg->MinMaxInfo->MaxHeight += y;
  98.  
  99.     return(0);
  100. }
  101.  
  102.  
  103. ULONG url_draw(struct IClass *cl, Object *obj, struct MUIP_Draw *msg)
  104. {
  105.     struct urldata *data = INST_DATA(cl,obj);
  106.     int x,y,i;
  107.  
  108.     DoSuperMethodA(cl,obj,(APTR)msg);
  109.     if(!(msg->flags & MADF_DRAWOBJECT)) return(0);
  110.  
  111.     SetFont(_rp(obj),_font(obj));
  112.  
  113.     if(!data->pixlen) data->pixlen=TextLength(_rp(obj),data->name,data->len);
  114.     x = _mleft(obj) + ( (_mwidth(obj) - data->pixlen) / 2);
  115.     y = _mtop(obj) + _font(obj)->tf_Baseline + ( (_mheight(obj) - _font(obj)->tf_YSize) / 2 );
  116.  
  117.     Move(_rp(obj),x,y);
  118.     if(data->color != -1) SetAPen(_rp(obj),data->color); else SetAPen(_rp(obj),1);
  119.     Text(_rp(obj),data->name,data->len);
  120.  
  121.     for (i=x; i<=x+data->pixlen; i+=4) {
  122.          Move(_rp(obj), i, y+3);
  123.         Draw(_rp(obj), i+1, y+3);
  124.     }
  125.  
  126.     return(0);
  127. }
  128.  
  129.  
  130. ULONG url_cleanup(struct IClass *cl, Object *obj, Msg msg)
  131. {
  132.     struct urldata *data = INST_DATA(cl,obj);
  133.  
  134.     if(data->color != -1) ReleasePen(_screen(obj)->ViewPort.ColorMap,data->color);
  135.  
  136.     return(DoSuperMethodA(cl,obj,msg));
  137. }
  138.  
  139.  
  140. ULONG url_clicked(struct IClass *cl, Object *obj, Msg msg)
  141. {
  142.     struct urldata *data = INST_DATA(cl,obj);
  143.  
  144.     OpenURLBase = OpenLibrary("openurl.library", 1);
  145.     if (!OpenURLBase) return(0);
  146.     URL_Open(data->href, TAG_DONE);
  147.     CloseLibrary(OpenURLBase);
  148.     return(0);
  149. }
  150.